package com.ebrothers.isns.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import android.util.Log;

import com.ebrothers.isns.common.API;
import com.ebrothers.isns.common.Constants;
import com.ebrothers.isns.common.Params;
import com.ebrothers.isns.util.HttpManager;
import com.ebrothers.isns.util.Utils;
import com.ebrothers.isns.vo.Answer;
import com.ebrothers.isns.vo.Question;

public class QsInteractor {
	private static final String TAG = "QsAsyncInteractor";

	public static void newAQuestion(Question q) {
		final HashMap<String, String> param = new HashMap<String, String>();
		param.put(Params.U_NAME, Utils.encode(q.owner.name));
		param.put(Params.Q_BODY, Utils.encode(q.body));
		param.put(Params.Q_TYPE, String.valueOf(q.type));
		param.put(Params.Q_POS, Utils.locationToString(q.triggerLocation));
		String url = API.getURL(API.NEW_QUESTION, param);
		if (Constants.LOGD) {
			Log.d(TAG, "****** newAQuestion. url=" + url);
		}
		final HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = HttpManager.execute(httpGet);
			int responseCode = response.getStatusLine().getStatusCode();
			final HttpEntity entity = response.getEntity();
			if (HttpStatus.SC_OK == responseCode && entity != null) {
				entity.consumeContent();
			} else {
				Log.e(TAG,
						"****** Error when posting a Question with status code is ["
								+ response.getStatusLine() + "]");
			}
		} catch (IOException e) {
			httpGet.abort();
			Log.e(TAG, "****** Got an error when interaction with server.", e);
		}
	}

	public static void answerAQuestion(Answer a) {
		final HashMap<String, String> params = new HashMap<String, String>();
		params.put(Params.Q_ID, String.valueOf(a.question.id));
		params.put(Params.U_NAME, Utils.encode(a.owner.name));
		params.put(Params.A_BODY, Utils.encode(a.body));
		params.put(Params.A_POS, Utils.locationToString(a.triggerLocation));
		String url = API.getURL(API.ANSWER_QUESTION, params);
		if (Constants.LOGD) {
			Log.d(TAG, "****** answerAQuestion. url=" + url);
		}
		final HttpGet httpGet = new HttpGet(url);
		try {

			HttpResponse response = HttpManager.execute(httpGet);
			int responseCode = response.getStatusLine().getStatusCode();
			final HttpEntity entity = response.getEntity();
			if (HttpStatus.SC_OK == responseCode && entity != null) {
				entity.consumeContent();
			} else {
				Log.e(TAG,
						"****** Error when answer a Question with status code is ["
								+ response.getStatusLine() + "]");
			}
		} catch (IOException e) {
			httpGet.abort();
			Log.e(TAG, "****** Got an error when interaction with server.", e);
		}
	}

	public static ArrayList<Question> getQuestions(String date) {
		String json = null;
		try {
			final HashMap<String, String> params = new HashMap<String, String>();
			params.put(Params.Q_DATE, date);
			json = HttpManager
					.getString(API.getURL(API.LIST_QUESTIONS, params));
			if (Constants.LOGD) {
				Log.d(TAG, "****** getQuestions. json=" + json);
			}
		} catch (IOException e) {
			Log.e(TAG, "****** Got an error when interaction with server.", e);
		}
		return JsonParser.parseQuestions(json);
	}

	public static ArrayList<Answer> getAnswers(long id) {
		String json = null;
		try {
			final HashMap<String, String> params = new HashMap<String, String>();
			params.put(Params.Q_ID, String.valueOf(id));
			json = HttpManager.getString(API.getURL(API.LIST_ANSWERS, params));
			if (Constants.LOGD) {
				Log.d(TAG, "****** getAnswers. json=" + json);
			}
		} catch (IOException e) {
			Log.e(TAG, "****** Got an error when interaction with server.", e);
		}
		return JsonParser.parseAnswers(json);
	}
}
